<?php
// read language file
if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'edit') {
$lines = file(TEMPLATEPATH.'/languages/'.$this->options['language']);
$matches = array();
$lang = array();
foreach($lines as $line) {
if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {
$lang[$matches[1]] = $matches[2];
}
}
}
// read language file

// save language file
if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'edit' && !empty($_POST['cp_save_language'])) {
if ($file = fopen(TEMPLATEPATH.'/languages/'.$this->options['language'], 'w')) {
fwrite($file, "<?php\r\n");
foreach($lang as $key => $value) {
$lang[$key] = $_POST[$key];
$updated_line = "DEFINE('".$key."', '".$_POST[$key]."');";
fwrite($file, "$updated_line\r\n");
}
fwrite($file, "?>");
}
fclose($file);
}
// save language file

// read language file (prior to make new file)
if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'new') {
$lines = file(TEMPLATEPATH.'/languages/'.$this->options['language']);
$matches = array();
$lang = array();
foreach($lines as $line) {
if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {
$lang[$matches[1]] = $matches[2];
}
}
}
// read language file (prior to make new file)

// writing new language file
if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'new' && !empty($_POST['cp_new_language']) && !empty($_POST['new_language']) && !file_exists(TEMPLATEPATH.'/languages/'.$_POST['new_language'].'.php') && $file = fopen(TEMPLATEPATH.'/languages/'.$_POST['new_language'].'.php', 'w')) {

fwrite($file, "<?php\r\n");
foreach($lang as $key => $value) {
$updated_line = "DEFINE('".$key."', '".$_POST[$key]."');";
fwrite($file, "$updated_line\r\n");
}
fwrite($file, "?>");
$seterror = false;
fclose($file);

} else {
$seterror = true;
}
// writing new language file

// deleting language
global $options;
if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'delete' && !empty($_POST['cp_delete_language'])) {
$dirPath = TEMPLATEPATH. '/languages/';
if ($handle = opendir($dirPath)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_file("$dirPath/$file")) {
$langfiles[] = $file;
}
}
}
closedir($handle);
}
if (!file_exists($dirPath.$this->options['language']) || $_GET['x'] == remove_ext($this->default_settings['language'])) {
$seterror = true;
} else {
unlink($dirPath.$this->options['language']);
$seterror = false;
}
}
// deleting language

if (!empty($_POST['cp_save'])) {
	$this->options["text_direction"] = $_POST['cp_text_direction'];
	$this->options["language"] = $_POST['cp_language'];
	update_option($cpanel['name'], $this->options);
	echo '<div class="updated fade msg" id="message"><p>Your changes have been saved.</p></div>';
}

if (!empty($_POST['cp_save_language'])) {
	echo '<div class="updated fade msg" id="message"><p>Language file has been edited successfully.</p></div>';
}

if (!empty($_POST['cp_new_language'])) {
	if ($seterror == true) {
	echo '<div class="error" id="message"><p>Failed to create new language file.</p></div>';
	} elseif ($seterror == false) {
	echo '<div class="updated fade msg" id="message"><p>New language file has been created successfully.</p></div>';
	}
}

if (!empty($_POST['cp_delete_language'])) {
	if ($seterror == true) {
	echo '<div class="error" id="message"><p>We were unable to delete this language. Are you trying to delete default language file?</p></div>';
	} elseif ($seterror == false) {
	echo '<div class="updated fade msg" id="message"><p>Language file has been deleted from the server.</p></div>';
	}
}
?>

<div class="cpage">
<h2>Manage Languages</h2>

<form action="" method="post">

<fieldset><legend>Localization</legend>
<?php if (file_exists(TEMPLATEPATH.'/styles/'.$this->options['style'].'/rtl.css')) : ?>
<ul>
	<li><label for="cp_text_direction">Text Direction</label></li>
	<li>
	<select name="cp_text_direction" class="widefat mid">
	<option value="ltr"<?php selected("ltr", $this->options["text_direction"]); ?>>Left to right</option>
	<option value="rtl"<?php selected("rtl", $this->options["text_direction"]); ?>>Right to left</option>
	</select>
	</li>
</ul>
<?php endif; ?>
<ul>
	<li><label for="cp_language">Choose from available languages</label></li>
	<li>
	<select name="cp_language" class="widefat mid">
	<?php
	$dirPath = TEMPLATEPATH. '/languages/';
	if ($handle = opendir($dirPath)) {
	while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
	if (is_file("$dirPath/$file")) {
	?>
	<option value="<?php echo $file; ?>"<?php selected($file, $this->options["language"]); ?>><?php echo ucfirst(remove_ext($file)); ?></option>
	<?php
	}
	}
	}
	closedir($handle);
	}
	?>
	</select>
	</li>
</ul>
</fieldset>

<input type="submit" value="Update Settings" name="cp_save" class="button-primary" />
</form>

<fieldset><legend>Manage (<?php echo ucfirst(remove_ext($this->options['language'])); ?>) Language</legend>
<ol>
	<li><a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo remove_ext($this->options['language']); ?>&amp;y=new">Add new language</a> (based on <?php echo ucfirst(remove_ext($this->options['language'])); ?>)</li>
	<li><a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo remove_ext($this->options['language']); ?>&amp;y=edit">Edit language</a></li>
	<li><a href="?page=<?php echo $_GET['page']; ?>&amp;x=<?php echo remove_ext($this->options['language']); ?>&amp;y=delete">Delete language</a></li>
</ol>
</fieldset>

<?php if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'edit' && empty($_POST['cp_save_language'])) { ?>
<form action="" method="post">
<fieldset><legend>Editing Language - <?php echo ucfirst(remove_ext($this->options['language'])); ?></legend>
<?php foreach ($lang as $key => $value) { ?>
<ul>
	<li><label for="<?php echo $key; ?>"><?php echo $key; ?></label></li>
	<li><input class="widefat large" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo stripslashes($value); ?>"></li>
</ul>
<?php } ?>
</fieldset>
<input type="submit" value="Update Language" name="cp_save_language" class="button" style="color:#000" />
</form>
<?php } ?>

<?php if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'new' && empty($_POST['cp_new_language'])) : ?>
<form action="" method="post">
<fieldset><legend>Create a New Language</legend>
<ul>
	<li><label for="new_language">LANGUAGE_UNIQUE_NAME</label></li>
	<li><input class="widefat large" name="new_language" id="new_language" value="LANGUAGE_NAME"></li>
</ul>
<?php foreach ($lang as $key => $value) { ?>
<ul>
	<li><label for="<?php echo $key; ?>"><?php echo $key; ?></label></li>
	<li><input class="widefat large" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo stripslashes($value); ?>"></li>
</ul>
<?php } ?>
</fieldset>
<input type="submit" value="Create Language" name="cp_new_language" class="button" style="color:#000" />
</form>
<?php endif; ?>

<?php if ($_GET['x'] == remove_ext($this->options['language']) && $_GET['y'] == 'delete' && empty($_POST['cp_delete_language'])) : ?>
<form action="" method="post">
<fieldset><legend>Deleting (<?php echo ucfirst(remove_ext($this->options['language'])); ?>) language!</legend>
<ol><li>Click on "confirm" to proceed with deleting this language. This action cannot be undone!</li></ol>
</fieldset>
<input type="submit" value="Confirm" name="cp_delete_language" class="button" style="color:#000" />
</form>
<?php endif; ?>

</div>